﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.Silverlight.Library;
using FRL.DebtChased.Silverlight.Library.RichContentService;
using FRL.DebtChased.Types;

namespace FRL.DebtChased.ChaseWorkspace
{

	/// <summary>
	/// Declaration of DebtSummaryCtrl class
	/// Summary heading bar for main page
	/// </summary>
	public partial class DebtSummaryCtrl : UserControl
	{

		/////////////////////////////////////////////////////////////
		// Construction

		public DebtSummaryCtrl()
		{

			// Load xaml resources
			InitializeComponent();

		}

		/////////////////////////////////////////////////////////////
		// Operations

		public void Set(AccountTransport account)
		{

			// Declaration of local variables
			DateTime dtNow = DateTime.Now;

			// Calculate average days overdue
			int iTotal = 0;
			for (int i1 = 0; i1 < account.AccountInvoices.Count; i1++)
			{
				TimeSpan span = dtNow - account.AccountInvoices[i1].InvoiceDate;
				int iDays = (int)Math.Floor(span.TotalDays);
				iTotal += iDays;
			}
			int iAverageDays = iTotal / account.AccountInvoices.Count;

			// Set controls
			trTransactionCount.Text = account.AccountInvoices.Count.ToString();
			trAvgDaysOverdue.Text = iAverageDays.ToString();

			// Get a list of currencies
			List<Money.eCurrency> currencyList = new List<Money.eCurrency>();
			for (int i1 = 0; i1 < account.AccountInvoices.Count; i1++)
			{
				Money.eCurrency currency = Money.ParseCurrencyCode(account.AccountInvoices[i1].Amount.CurrencyCode);
				if (!currencyList.Contains(currency))
					currencyList.Add(currency);
			}

			// Add and remove rows
			while (LayoutRoot.RowDefinitions.Count < (currencyList.Count + 1))
				_addRow();
			while (LayoutRoot.RowDefinitions.Count > (currencyList.Count + 1))
				_removeRow();

			// Loop through currency list
			for (int i1 = 0; i1 < currencyList.Count; i1++)
			{

				// Get the next currency
				Money.eCurrency currency = currencyList[i1];

				// Calculate values
				decimal dcDebtPassed = 0;
				decimal dcPaid = 0;
				for (int i2 = 0; i2 < account.AccountInvoices.Count; i2++)
				{
					if (Money.ParseCurrencyCode(account.AccountInvoices[i2].Amount.CurrencyCode) == currency)
					{
						dcDebtPassed += account.AccountInvoices[i2].Amount.Amount;
						dcPaid += 0;
					}
				}
				decimal dcLPCDA = (dcDebtPassed * 8) / 100;
				decimal dcTotal = dcLPCDA + dcPaid;

				// Set controls
				TextRectangleCtrl trDebtPassed = null;
				TextRectangleCtrl trLPCDA = null;
				TextRectangleCtrl trPaid = null;
				TextRectangleCtrl trTotal = null;
				_getControls(i1 + 1, ref trDebtPassed, ref trLPCDA, ref trPaid, ref trTotal);
				trDebtPassed.Text = new Money(dcDebtPassed, currency).Format(true);
				trLPCDA.Text = new Money(dcLPCDA, currency).Format(true);
				trPaid.Text = new Money(dcPaid, currency).Format(true);
				trTotal.Text = new Money(dcTotal, currency).Format(true);

			}

		}

		private void _addRow()
		{

			// Add row definitions
			RowDefinition row = new RowDefinition();
			row.Height = new GridLength(0, GridUnitType.Auto);
			LayoutRoot.RowDefinitions.Add(row);

			// Calc new row index
			int iRowIndex = LayoutRoot.RowDefinitions.Count - 1;

			// Add controls
			TextRectangleCtrl trDebtPassed = new TextRectangleCtrl();
			trDebtPassed.Style = this.Resources["DynamicRectangleStyle"] as Style;
			LayoutRoot.Children.Add(trDebtPassed);
			Grid.SetColumn(trDebtPassed,6);
			Grid.SetRow(trDebtPassed,iRowIndex);

			TextRectangleCtrl trLPCDA = new TextRectangleCtrl();
			trLPCDA.Style = this.Resources["DynamicRectangleStyle"] as Style;
			LayoutRoot.Children.Add(trLPCDA);
			Grid.SetColumn(trLPCDA, 8);
			Grid.SetRow(trLPCDA, iRowIndex);

			TextRectangleCtrl trPaid = new TextRectangleCtrl();
			trPaid.Style = this.Resources["DynamicRectangleStyle"] as Style;
			LayoutRoot.Children.Add(trPaid);
			Grid.SetColumn(trPaid, 10);
			Grid.SetRow(trPaid, iRowIndex);

			TextRectangleCtrl trTotal = new TextRectangleCtrl();
			trTotal.Style = this.Resources["DynamicRectangleStyle"] as Style;
			LayoutRoot.Children.Add(trTotal);
			Grid.SetColumn(trTotal, 12);
			Grid.SetRow(trTotal, iRowIndex);

		}

		private void _removeRow()
		{

			// Call extension function
			LayoutRoot.DeleteRows(LayoutRoot.RowDefinitions.Count - 1, 1);

		}

		private void _getControls(int iRowIndex, ref TextRectangleCtrl trDebtPassed, ref TextRectangleCtrl trLPCDA, ref TextRectangleCtrl trPaid, ref TextRectangleCtrl trTotal)
		{

			// Reset function parameters
			trDebtPassed = null;
			trLPCDA = null;
			trPaid = null;
			trTotal = null;
			foreach (FrameworkElement el in LayoutRoot.Children)
			{

				if (Grid.GetRow(el) == iRowIndex)
				{

					int iColumn = Grid.GetColumn(el);
					if (iColumn < 6)
					{

					}
					else if (iColumn == 6)
					{
#if DEBUG
						Debug.Assert(trDebtPassed == null);
#endif // #if DEBUG
						trDebtPassed = el as TextRectangleCtrl;
					}
					else if (iColumn == 8)
					{
#if DEBUG
						Debug.Assert(trLPCDA == null);
#endif // #if DEBUG
						trLPCDA = el as TextRectangleCtrl;
					}
					else if (iColumn == 10)
					{
#if DEBUG
						Debug.Assert(trPaid == null);
#endif // #if DEBUG
						trPaid = el as TextRectangleCtrl;
					}
					else if (iColumn == 12)
					{
#if DEBUG
						Debug.Assert(trTotal == null);
#endif // #if DEBUG
						trTotal = el as TextRectangleCtrl;
					}
#if DEBUG
					else
						Debug.Assert(false);
#endif // #if DEBUG

				}

			}

		}

	}

}
